﻿using System;
using System.Collections.Generic;

namespace Oscilloscope.ChannelBuffer
{
	// Token: 0x0200002B RID: 43
	public class ChannelBufferManager : IChannelBufferManager
	{
		// Token: 0x0600013E RID: 318 RVA: 0x00009A94 File Offset: 0x00007C94
		public ChannelBufferManager(int channelCount)
		{
			if (channelCount < 0)
			{
				throw new ArgumentOutOfRangeException("channelCount");
			}
			this.channelCount = channelCount;
			this.buffers = new IChannelBuffer[channelCount];
			for (int i = 0; i < channelCount; i++)
			{
				this.buffers[i] = ChannelBufferFactory.EmptyChannelBuffer();
			}
			this.readwritelock = new object();
			this.isAddChannelData = true;
		}

		// Token: 0x0600013F RID: 319 RVA: 0x00009AF6 File Offset: 0x00007CF6
		public IChannelBuffer GetChannelBuffer(int channelSN)
		{
			if (channelSN < 1 || channelSN > this.channelCount)
			{
				throw new ArgumentOutOfRangeException();
			}
			return this.buffers[channelSN - 1];
		}

		// Token: 0x06000140 RID: 320 RVA: 0x00009B18 File Offset: 0x00007D18
		public void SetChannelBuffer(int channelSN, IChannelBuffer channelBuffer)
		{
			if (channelSN < 1 || channelSN > this.channelCount)
			{
				throw new ArgumentOutOfRangeException("channelSN");
			}
			if (channelBuffer == null)
			{
				throw new ArgumentNullException("channelBuffer");
			}
			this.buffers[channelSN - 1].Dispose();
			this.buffers[channelSN - 1] = channelBuffer;
			this.RecoverAddChannelData();
		}

		// Token: 0x06000141 RID: 321 RVA: 0x00009B6C File Offset: 0x00007D6C
		public void SetChannelBuffer(IDictionary<int, IChannelBuffer> channelBuffers)
		{
			foreach (int num in channelBuffers.Keys)
			{
				if (num >= 1 && num <= this.channelCount)
				{
					this.buffers[num - 1].Dispose();
					this.buffers[num - 1] = channelBuffers[num];
				}
			}
			this.RecoverAddChannelData();
		}

		// Token: 0x06000142 RID: 322 RVA: 0x00009BE8 File Offset: 0x00007DE8
		public object GetReadWriteLocker()
		{
			return this.readwritelock;
		}

		// Token: 0x06000143 RID: 323 RVA: 0x00009BF0 File Offset: 0x00007DF0
		public void AddChannelData(short?[] sampleFrameData)
		{
			if (!this.isAddChannelData)
			{
				return;
			}
			if (sampleFrameData.Length != this.channelCount)
			{
				throw new ArgumentException("sampleFrameData.length != channelCount");
			}
			lock (this.readwritelock)
			{
				for (int i = 0; i < this.channelCount; i++)
				{
					this.buffers[i].AddSampleData(sampleFrameData[i]);
				}
			}
		}

		// Token: 0x06000144 RID: 324 RVA: 0x00009C70 File Offset: 0x00007E70
		public void StopAddChannelData()
		{
			this.isAddChannelData = false;
		}

		// Token: 0x06000145 RID: 325 RVA: 0x00009C7B File Offset: 0x00007E7B
		public void RecoverAddChannelData()
		{
			this.isAddChannelData = true;
		}

		// Token: 0x040000A0 RID: 160
		private int channelCount;

	/// <summary>
	/// 不同通道的Buffer
	/// </summary>
		public IChannelBuffer[] buffers;

		// Token: 0x040000A2 RID: 162
		private object readwritelock;

		// Token: 0x040000A3 RID: 163
		private volatile bool isAddChannelData;
	}
}
